<?php

namespace Home\Controller;

use Think\Controller;
use Home\Model\TougaoModel;
use Home\Model\CategoryModel;
use Home\Model\UserModel;

class TougaoController extends Controller {
	public function _initialize() {
		$this->uid = get_cookie ( 'uid' );
		if ($this->uid) {
		} else {
			$this->error ( '请登录后再操作!' );
		}
	}
	public function index() {
		$data ['nav'] = 'article';
		$do = I ( 'get.do' );
		$id = I ( 'get.id' );
		$page = I ( 'get.p' );
		$article = new TougaoModel ();
		$category = new CategoryModel ();
		$where = 'type="article"';
		$data ['category'] = $category->search ( $where );
		if ($do == 'add') {
			$where = 'uid=' . intval ( $this->uid ) . ' and createtime like "' . date ( 'Y-m-d' ) . '%"';
			$num = $article->where ( $where )->count ();
			if ($num > 15) {
				$this->error ( '今天发表文章大于' . $num . '篇,请明天再来发表~' );
			}
			if ($id) { // 编辑页面
				$data ['info'] = $article->getInfo ( $id );
				if ($data ['info'] ['status'] >= 1) {
					$this->error ( '文章已经上线,不能编辑' );
				}
				if ($data ['info'] ['uid'] != $this->uid) {
					$this->error ( '没有权限' );
				}
			}
		} else if ($do == 'save') { // 保存
			$rs = $article->saveData ( $_POST, $_POST ['id'] );
			if ($rs ['status']) {
				$this->success ( $rs ['info'], '/?c=tougao' );
			} else {
				$this->error ( $rs ['info'] );
			}
			exit ();
		} else {
			$where = 'uid=' . $this->uid;
			$data ['article'] = $article->search ( $where, $page, ' updatetime desc' );
		}
		$data ['seo'] ['title'] = '投稿管理';
		$data ['do'] = $do;
		$this->data = $data;
		$this->display ();
	}
}
